package com.cily.utils.t_webrtc.singlr.Commons;

/**
 * Created by tim on 2017/8/27.
 */


import android.content.Context;
import android.widget.Toast;

import com.google.gson.JsonElement;

import microsoft.aspnet.signalr.client.Connection;
import microsoft.aspnet.signalr.client.ErrorCallback;
import microsoft.aspnet.signalr.client.MessageReceivedHandler;
import microsoft.aspnet.signalr.client.StateChangedCallback;
import microsoft.aspnet.signalr.client.transport.ClientTransport;
import microsoft.aspnet.signalr.client.transport.ServerSentEventsTransport;

/**
 * Created by tim on 2017/8/24.  Fort test
 */

public class SignalRConnection {
    public  Connection mConnection;
    private  Context context;
    public  String mConnectionID;

    private final  String SIGNALR_URL = "http://47.90.73.137:9003/SR";
    public SignalRConnection(Context context) {
        this.context = context;
    }
    public  void startSignalR() {
        try {

            mConnection = new Connection(SIGNALR_URL){
                @Override
                protected void onConnected() {
                    super.onConnected();
                    Toast.makeText(context, "Message: "+ this.getState().toString() , Toast.LENGTH_LONG).show();
                }

                @Override
                protected void onClosed() {
                    super.onClosed();
                }

                @Override
                public void received(MessageReceivedHandler handler) {
                    super.received(handler);
                }

                @Override
                public void onReceived(JsonElement message) {
                    super.onReceived(message);

                    Toast.makeText(context, "Message: " + message, Toast.LENGTH_LONG).show();
                }

                @Override
                public void error(ErrorCallback handler) {
                    super.error(handler);
                }

                @Override
                public void connectionSlow(Runnable handler) {
                    super.connectionSlow(handler);
                }

                @Override
                public void stateChanged(StateChangedCallback handler) {
                    super.stateChanged(handler);
                }
            };
            ClientTransport clientTransport = new ServerSentEventsTransport(mConnection.getLogger());
            mConnection.start(clientTransport);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

